# -*- coding: utf-8 -*-
import scrapy


class MmonlyGroupListSpider(scrapy.Spider):
    name = 'mmonly_group_list'
    allowed_domains = ['www.mmonly.cc']
    start_urls = ['http://www.mmonly.cc/tag/lj/index.html']

    def parse(self, response):
        elems = response.xpath('//div[@class="item masonry_brick masonry-brick"]')
        for elem in elems:
            item = {}
            image_url = elem.xpath('./div/div/div/a/img/@src').extract()
            group_image_url = elem.xpath('./div/div/div/a/@href').extract()
            item['image_url'] = image_url[0]
            item['group_image_url'] = group_image_url[0]
            item['tag_id'] = ",".join(
                group_image_url[0].replace('http://www.mmonly.cc/', "").replace('.html', "").split('/'))
            des = elem.xpath('./div/div/span/a/text()').extract()
            if len(des) == 0:
                des = elem.xpath('./div/div/span/a/b/text()').extract()
            item['des'] = des[0]
            tag = elem.xpath('./div/div/a')
            item['tag'] = "" if len(tag) == 0 else tag.xpath('./text()').extract()[0]
            time = elem.xpath('./div/div[@class="items_likes"]/text()').extract()
            item['time'] = time[0]
            yield item
        # 查找下一页
        elem_pages = response.xpath('//div[@class="pages"]/ul/li')
        for elem in elem_pages:
            if len(elem.xpath('./@class').extract()) > 0 and \
                    elem.xpath('./@class').extract()[0] == 'thisclass' and \
                    elem_pages.index(elem) + 1 < len(elem_pages):
                next_url = "http://www.mmonly.cc" + elem_pages[elem_pages.index(elem) + 1].xpath('./a/@href').extract()[0]
                print("\n\n\n********" + next_url + "*******\n")
                yield scrapy.Request(
                    next_url,
                    callback=self.parse
                )
